#include "string.h"
#include "types.h"

extern void printk(char *fmt, ...);

int memncpy(void *srcAddr, void *desAddr, unsigned int nBytes)
{
    if ((srcAddr == NULL) || (desAddr == NULL))
    {
        return (ERR_RET);
    }
    unsigned int i = 0;
    for (i = 0; i < nBytes; i++)
    {
        // printk("srcAddr[i]:%c\n",*((char *)srcAddr + i));
        *((char *)(desAddr) + i) = *((char *)srcAddr + i);
    }
    return (SUC_RET);
}

int memnset(void *addr, char c, unsigned int bytes)
{
    if ((addr == NULL) || (c < 0))
    {
        return (ERR_RET);
    }
    unsigned int i = 0;
    for (i = 0; i < bytes; i++)
    {
        *((char *)addr + i) = c;
    }
    return (SUC_RET);
}

int memncmp(void *p1, void *p2, unsigned int bytes)
{
    if ((p1 == NULL) || (p2 == NULL))
    {
        return (ERR_RET);
    }
    unsigned int i = 0;
    for (i = 0; i < bytes; i++)
    {
        if ((*((char *)p1 + i)) != (*((char *)p2 + i)))
        {
            return (ERR_RET);
        }
    }
    return (SUC_RET);
}

unsigned int strlen(char *string)
{
    if (string == NULL)
    {
        return (ERR_RET);
    }
    unsigned int n = 0;
    for (;;)
    {
        if (*((char *)(string + n)) == '\0')
        {
            return (n);
        }
        n++;
    }
}

// --------------------------------------------------
// buf2:hello
// pass
void memncpy_test(void)
{
    char buf1[] = "hello";
    char buf2[6];
    int ret = ERR_RET;
    ret = memncpy(buf1, buf2, 5);
    if (ret == ERR_RET)
    {
        printk("memncpy error\n");
        return;
    }
    printk("buf2:%s\n", buf2);
}

// after memnset buf:aaaaaaa
// pass
void memnset_test(void)
{
    char buf[8];
    int ret = ERR_RET;
    ret = memnset(buf, 'a', 7);
    if (ret == ERR_RET)
    {
        printk("memnset error\n");
        return;
    }
    printk("after memnset buf:%s\n", buf);
}

void memncmp_test(void)
{
}